package cn.backpackerxl.test;

import cn.backpackerxl.easyJDBC.util.*;
import cn.backpackerxl.entity.Admin;
import cn.backpackerxl.entity.Source;
import cn.backpackerxl.entity.Teacher;
import cn.backpackerxl.pojo.Comment;
import cn.backpackerxl.pojo.CommentPacking;
import cn.backpackerxl.pojo.StudentSource;
import cn.backpackerxl.service.AdminService;
import cn.backpackerxl.service.StudentService;
import cn.backpackerxl.service.impl.AdminServiceImpl;
import cn.backpackerxl.service.impl.StudentServiceImpl;
import org.junit.Test;

import java.io.IOException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static cn.backpackerxl.easyJDBC.util.MD5Passwd.code;

/**
 * @author: backpackerxl
 * @create: 2021/12/23
 * @filename: EasyJDBC
 **/
public class EasyJDBC {

    /**
     * 测试🔗
     *
     * @throws SQLException
     */
    @Test
    public void testConn() throws SQLException {
        System.out.println(EasyJDBCUtil.getConnection());
    }

    /**
     * 测试添加数据
     */
    @Test
    public void testSave() {
        Admin admin = new Admin();
        admin.setName("王桑");
        admin.setPasswd("14725812");
        admin.setCreateTime(new Date());
        AdminService adminService = new AdminServiceImpl();
//        for (int i = 0; i < 100; i++) {
        int save = adminService.save(admin);
        System.out.println(save);
//        }
    }

    /**
     * 测试修改数据
     */
    @Test
    public void testUpdate() {
        Admin admin = new Admin();
        admin.setId(3);
        admin.setName("李四");
        admin.setPasswd("945946");
        AdminService adminService = new AdminServiceImpl();
        for (int i = 0; i < 100; i++) {
            admin.setCreateTime(new Date());
            int update = adminService.update(admin);
            System.out.println(update);
        }
    }

    /**
     * 测试删除数据
     */
    @Test
    public void testRemove() {
        AdminService adminService = new AdminServiceImpl();
        for (int i = 16; i <= 115; i++) {
            int remove = adminService.remove((long) i);
            System.out.println(remove);
        }
    }

    /**
     * 测试查询数据
     */
    @Test
    public void testFind() {
        AdminService adminService = new AdminServiceImpl();
        Admin admin = adminService.findById(5L);
        System.out.println(admin);
        List<Admin> all = adminService.findAll();
        System.out.println(JSON.stringify(all));
        Admin other = adminService.findByNameAndPasswd("张三", "945946");
        System.out.println(other);
        System.out.println(adminService.findByNameOrPasswd("张三", code("945946", 32)));
    }

    /**
     * 测试分页
     */
    @Test
    public void testPage() {
        AdminService adminService = new AdminServiceImpl();
        PageBean<Admin> pageBean = adminService.pageEntityAll(1, 2);
        System.out.println(pageBean);
    }

    /**
     * 测试搜索
     */
    @Test
    public void testSearch() {
        AdminService adminService = new AdminServiceImpl();
        System.out.println(adminService.searchByName("王五"));
        System.out.println(adminService.searchByName("李四", 1, 1));
    }

    /**
     * 测试按不同的条件进行查询
     */
    @Test
    public void testFindByOne() {
        AdminService adminService = new AdminServiceImpl();
        for (int i = 0; i < 1000; i++) {
            System.out.println(adminService.findByName("李四"));
        }
    }

    /**
     * 测试连接查询
     */
    @Test
    public void testJoin() {
        StudentService service = new StudentServiceImpl();
        System.out.println(service.findStudentLeftSource());
        List<StudentSource> list = service.findStudentLeftSourceByName("胡洋");
        for (StudentSource studentSource : list) {
            System.out.println(studentSource.getName());
            System.out.println(studentSource.getHadoop());
            System.out.println(studentSource.getHive());
            System.out.println(studentSource.getJavaWeb());
        }
    }

    @Test
    public void testJoinType() {
        StudentService service = new StudentServiceImpl();
        List<StudentSource> list = service.findStudentLeftSourceByName("hy");
        List<StudentSource> list2 = service.findStudentLeftSource();
        System.out.println(JSON.stringify(list2));
        for (StudentSource studentSource : list) {
            System.out.println(studentSource.getName());
            System.out.println(studentSource.getHadoop());
            System.out.println(studentSource.getHive());
            System.out.println(studentSource.getJavaWeb());
        }
    }

    /**
     * 测试连接查询
     */
    @Test
    public void testQuery() {
        StudentService service = new StudentServiceImpl();
        String name = "hy";
        int sex = 0;
        for (int i = 0; i < 100; i++) {
            System.out.println(service.findByQuery(name, sex));
        }
    }

    @Test
    public void testJSON() {
        AdminService adminService = new AdminServiceImpl();
        Admin admin = adminService.findById(3L);
        List<Admin> all = adminService.findAll();
        System.out.println(JSON.stringify(admin));
        System.out.println(JSON.stringify(all));
        String json = "  { \"id\":1,\"name\":\"李四\",\"passwd\":\"b5f2de88ad390170afe477fb31f75b5d\",\"create_time\":1641804131000   } ";
        Admin a = JSON.setValid(Admin.class).parseValue(json);
        System.out.println(a);
    }


    @Test
    public void testTeacher() {
        Teacher teacher = new Teacher();
        teacher.setName(null);
        teacher.setClassTime((short) 77);
        teacher.setFlag(false);
        teacher.setInformation("ggg1 !!!");
        teacher.setSex('v');
        String json = JSON.stringify(teacher);
        System.out.println(json);
        Teacher teacher1 = JSON.setValid(Teacher.class).parseValue(json);
        System.out.println(teacher1);
    }

    @Test
    public void packJSON() {
        Comment comment = new Comment(1L, null, "hi", new Date(), 0L);
        Comment comment1 = new Comment(2L, "jack", "hello", new Date(), 0L);
        Comment comment2 = new Comment(3L, "alice", "ok", new Date(), 0L);
        Comment comment3 = new Comment(4L, "peter", "i know", new Date(), 0L);
        List<Comment> comments = new ArrayList<>();
        List<Comment> comments2 = new ArrayList<>();
        List<Comment> comments3 = new ArrayList<>();
        comments.add(comment1);
        comments.add(comment2);
        comments.add(comment3);
//        System.out.println(JSON.stringify(comment));
//        System.out.println(JSON.stringify(comments));
        comments2.add(comment3);
        comments3.add(comment2);
        CommentPacking commentPacking = new CommentPacking(1, 4001L, null, 'a', 5L, true, null, null);
        CommentPacking commentPacking1 = new CommentPacking(2, 4002L, null, 'b', 5L, true, comment1, comments2);
        CommentPacking commentPacking2 = new CommentPacking(3, 4003L, "king2", 'c', 5L, false, comment2, comments3);
        List<CommentPacking> comments4 = new ArrayList<>();
        comments4.add(commentPacking);
        comments4.add(commentPacking1);
        comments4.add(commentPacking2);
//        System.out.println(JSON.stringify(commentPacking));
        for (int i = 0; i < 100; i++) {
            System.out.println(JSON.stringify(comments4));
        }
    }


    @Test
    public void jsonToData() throws IOException {
        String json = "[{\"id\":2,\"name\":\"jack\",\"context\":\"hello\",\"create_time\":1651158133623,\"replay_id\":0},{\"id\":3,\"name\":\"alice\",\"context\":\"ok\",\"create_time\":1651158133623,\"replay_id\":0},{\"id\":4,\"name\":\"peter\",\"context\":\"i know\",\"create_time\":1651158133623,\"replay_id\":0}]";
        String json1 = "{\"id\":12,\"name\":\"tom\",\"context\":null,\"create_time\":1651158133623,\"replay_id\":0}";
        Comment comment = JSON.setValid(Comment.class).parseValue(json1);
        System.out.println(comment);

        List<Comment> comments = JSON.setValid(Comment.class).parseValueAll(json);

        System.out.println("--------------------------");
        System.out.println(comments);

        System.out.println("--------------------------");

        String str = "[{\"id\":1,\"name\":\"jack\",\"information\":\"perfect !!!\",\"flag\":true,\"sex\":\"男\",\"class_time\":75},{\"id\":2,\"name\":\"peter\",\"information\":\"haha\",\"flag\":false,\"sex\":\"女\",\"class_time\":85},{\"id\":3,\"name\":\"胡洋\",\"information\":\"呵呵\",\"flag\":true,\"sex\":\"男\",\"class_time\":85}]";
        List<Teacher> teachers = JSON.setValid(Teacher.class).parseValueAll(str);
        Path path = Paths.get("D:\\user\\backpackerxl\\easyJDBC\\src\\main\\resources\\teachers.json");
        JSON.save(path, teachers);
        System.out.println(teachers);
    }

    @Test
    public void source() throws IOException {
        Source source = new Source();
        source.setId(1);
        source.setName("hy");
        source.setHadoop(35);
        source.setJavaWeb(17);
        source.setHive(79);
//        Source all = easyJDBCExecutor.findById(1);
//        System.out.println(DataToJSON(all));
        JSON.save(Paths.get("D:\\user\\backpackerxl\\easyJDBC\\src\\main\\resources\\source.json"), source);
    }

    @Test
    public void testJSONFile() throws IOException {
        JSONObject parse = JSON.load("app.config.json").parse();
//        URL url = new URL("file:\\\\\\D:\\user\\backpackerxl\\easyJDBC\\src\\main\\resources\\app.config.json");
//        URL url1 = new URL("file:\\\\\\D:\\user\\backpackerxl\\easyJDBC\\src\\main\\resources\\source.json");
//        JSONObject parse = JSON.load(url).parse();
        Map<String, JSONObject> objValue = parse.getObjValue();
        System.out.println(objValue.get("tom_beer").getObjValue().get("port").getIntValue());
        Source source = JSON.load("source.json", Source.class).parseValue();

        List<Teacher> teachers = JSON.load("teachers.json", Teacher.class).parseValueAll();
        System.out.println(source);
        System.out.println(teachers);
    }


}




